/**
* 登录模块，提供登录相关业务功能
* @Date:    2015-6-16
* @author:  Jackson wang
* @version: 1.0
* @since:   1.0
*/
app.addModule('login', function(context) {

	'use strict';

	var formServ,
		ajaxServ,
		msgServ,
		moduleEl,
		formEl,
		private_method=0,
		formData=0;

	private_method = {
		loginHandler:function(event){			

			var _opt = {url:'/biz/login',data:formData,callback:private_method.loginOK,contentType:'form'};
			

			ajaxServ.runAjax(_opt);
			 
		},
		loginOK:function(data,ressionStr,statusObj){
			
			if(statusObj){
				switch(statusObj.status){
					case 204:
						msgServ.Msgslide("用户名或密码错误");
						break;
					case '204':
						msgServ.Msgslide("用户名或密码错误");
						break;
					case 200:
						window.location.href='/biz/home.html';
						break;
					case '200':
						window.location.href='/biz/home.html';
						break;
				}
			
			}
			//alert('login ok !!!');
		}


	};
	return {
		/**
		*
		* 初始化方法，进行模块内部的初始化工作		
		*/
		init:function(){
			ajaxServ = context.getService('ajax-serv');
			msgServ = context.getService('msg-serv');
		},

		/**
		*
		* 销毁方法，清除遗留数据及方法绑定，释放内存		*
		*/
		destory:function(){
			formServ = null;
			moduleEl = null;
			formEl = null;
			formData = null;
			msgServ = null;
		},

		/**
		 *
		 * 登录按钮点击handle
		 * @param {Event} 典型 DOM event 对象
		 * @param {HTMLElement} element The nearest HTML element with a data-type
		 *      attribute specified or null if there is none.
		 * @param {string} elementType The value of data-type for the nearest
		 *      element with that attribute specified or null if there is none.
		 * @returns {void}*
		*/
		onclick:function(event, element, elementType){
			if (elementType === 'submit-btn') {
				moduleEl = context.getElement();
				formEl = moduleEl.querySelector('form');

				formServ = context.getService('form-serv');
				formData = formServ.getDataObj({form:formEl});

				var rules,
					messages,
					validateOptions;
					
				
				rules = {username:"required"};
				messages = {username:'用户名不能为空'};
				validateOptions={
					debug:true,
					targetForm:formEl,
					rules:rules,
					messages:messages,
					submitHandler:private_method.loginHandler
				};
				
				formServ.onSubmit(validateOptions);
				
				

				//isValid = formServ.validate(validateOptions);

				//context.broadcast('todostatuschange');
			}
		}
		

	};

});